import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//enum是一个对象
public enum TestEnum {
    RED("红色",1),BLACK("黑色",2),GREEN("绿色",3),WHITE("白色",4);
    public String color;
    public int ordinal;

    //枚举的构造方法默认是私有的
    //枚举是不能被继承的
    //枚举是不能被实例化的
    //是否能通过反射实例化对象//不能 原码层次上杜绝了
    //单例模式：只能实例化一个对象
//当我们写一个枚举类的时候
    TestEnum(String color,int ordinal){
        this.color = color;
        this.ordinal =ordinal;
    }
    public static void reflectPrivateConstructor(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("TestEnum");
            Constructor constructor = c1.getDeclaredConstructor(String.class,int.class,String.class,int.class);
            constructor.setAccessible(true);
            TestEnum testEnum = (TestEnum) constructor.newInstance("haha",99);
            System.out.println(testEnum);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }
    public static void main3(String[] args) {
        //RED 枚举里面的东西属于对象
        System.out.println(RED.compareTo(GREEN));
    }
    public static void main2(String[] args) {
//        TestEnum[] testEnums = TestEnum.values();
//        for (int i = 0; i < testEnums.length; i++) {
//            System.out.println(testEnums[i] + "序号： " + testEnums[i].ordinal());
//        }
        //把字符串转换为枚举常量
        TestEnum testEnum = TestEnum.valueOf("RED2");
        System.out.println(testEnum);
    }
    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch (testEnum){
            case RED:
                System.out.println("RED");
                break;
            case BLACK:
                System.out.println("Black");
                break;
            case GREEN:
                System.out.println("GREEN");
                break;
            case WHITE:
                System.out.println("WHITE");
                break;
            default:
                System.out.println();
                break;
        }
    }
}
